﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using LS.Common;
using LS.Entitys;
namespace LS.Cores
{
    /// <summary>
    /// 提供菜单管理服务
    /// </summary>

    public class SysMenuService : BaseService<SysMenuEntity>, ISysMenu
    {
       
        public IList<SysMenuFunctionEntity> GetMenuFunction(string strwhere)
        {
            string sql = "select * From  sys_menufunction where 1=1 ";
            if (!string.IsNullOrEmpty(strwhere))
            {
                sql += strwhere;
            }
            return SqlHelper.Query<SysMenuFunctionEntity>(sql).ToList();
        }

        public int InsertMenuFun(SysMenuEntity model, List<SysMenuFunctionEntity> listModel)
        {
            bool resulte = SqlHelper.ExecuteTranSql((con, tran) =>
            {
                int r = 0;
                try
                {
                    StringBuilder strsql = new StringBuilder();
                    strsql.Append("insert into sys_menu(menucode,parentid,menuname,menuurl,menuicon,menuseq,isvisible,isenable,menulevel,createby,createdate,remark,targettype,isdel) ");
                    strsql.Append(" values(@menucode,@parentid,@menuname,@menuurl,@menuicon,@menuseq,@isvisible,@isenable,@menulevel,@createby,@createdate,@remark,@targettype,@isdel)");
                  
                    r = SqlHelper.ExecuteCon(strsql.ToString(), model,tran,null, CommandType.Text,con);
                 
                    if(listModel!=null && listModel.Count > 0)
                    {
                        StringBuilder strsqlfun = new StringBuilder();
                        strsqlfun.Append("insert into sys_menufunction(menucode,funcode) ");
                        strsqlfun.Append(" values(@menucode,@funcode)");
                        r = SqlHelper.ExecuteCon(strsqlfun.ToString(), listModel, tran, null, CommandType.Text, con);
                    }
                }
                catch (Exception ex)
                {
                    Log4NetHelper.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType).Debug(ex.Message);
                    r = 0;
                }

                return r;
            });
            return resulte ? 1 : 0;
        }

        public int UpdateMenuFun(SysMenuEntity model, List<SysMenuFunctionEntity> listModel)
        {
            bool resulte = SqlHelper.ExecuteTranSql((con, tran) =>
            {
                int r = 0;
                try
                {
                    StringBuilder strsql = new StringBuilder();
                    strsql.Append("update sys_menu set  ");
                    strsql.Append("menucode=@menucode,parentid=@parentid,menuname=@menuname,menuurl=@menuurl,menuicon=@menuicon,menuseq=@menuseq,isvisible=@isvisible,isenable=@isenable,menulevel=@menulevel,createby=@createby,createdate=@createdate,remark=@remark,targettype=@targettype");
                    strsql.Append(" where id=@id ");
                    r = SqlHelper.ExecuteCon(strsql.ToString(), model, tran, null, CommandType.Text, con);

                    if (listModel != null && listModel.Count > 0)
                    {
                        string sqlfun = "delete from  sys_menufunction where menucode=@Menucode";
                        r = SqlHelper.ExecuteCon(sqlfun.ToString(), new { Menucode = model.Menucode }, tran, null, CommandType.Text, con);
                        StringBuilder strsqlfun = new StringBuilder();
                        strsqlfun.Append("insert into sys_menufunction(menucode,funcode) ");
                        strsqlfun.Append(" values(@menucode,@funcode)");
                        r = SqlHelper.ExecuteCon(strsqlfun.ToString(), listModel, tran, null, CommandType.Text, con);
                    }
                }
                catch (Exception ex)
                {
                    Log4NetHelper.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType).Error(ex.Message);
                    r = 0;
                }

                return r;
            });
            return resulte ? 1 : 0;
        }

        public Hashtable GetMaxPartMenu(int parentId)
        {
            Hashtable hs = new Hashtable();
            string sql = "select isnull(max(MenuSeq),0) MenuSeq  From sys_menu ";
            if (parentId==0)
            {
                sql += " where Isdel=0 and  menulevel=0 ";
            }
            else
            {
                sql += " where Isdel=0 and parentid='" + parentId + "' ";
            }
            var model = SqlHelper.Query(sql).FirstOrDefault();
            foreach (var it in model)
            {
                if (it.Key == "MenuSeq")
                {
                    hs.Add("MenuSeq", it.Value);
                }

            }
            return hs;
        }

        public List<SysMenuEntity> GetUserPartMenu(string userCode, int mlevel)
        {

            return SqlHelper.Query<SysMenuEntity>("p_getUserMenu", new { UserCode = userCode }, commandType: CommandType.StoredProcedure).ToList();
          
        }
        /// <summary>
        /// 获取菜单
        /// </summary>
        /// <returns></returns>
        public IEnumerable<dynamic> GetMenuList(string strwhere)
        {
            StringBuilder strsql = new StringBuilder();
            strsql.Append("select A.*,");
            strsql.Append("dbo.f_menufunction(A.menucode) as funcode ");
            strsql.Append("from sys_menu A ");
            if (!string.IsNullOrEmpty(strwhere))
            {
                strsql.Append(" where 1=1  " + strwhere);
            }
            strsql.Append("order by parentid, menuseq");
            return SqlHelper.Query<dynamic>(strsql.ToString());
        }
        public int SetParentMenu(int menuId,int setMenuId)
        {
            var p = new DynamicParameters();
            p.Add("menuId", menuId);
            p.Add("setMenuId", setMenuId);
            p.Add("returnValue", dbType: DbType.Int32, direction: ParameterDirection.ReturnValue);
            SqlHelper.Execute("p_setMenuParentId", p, commandType: CommandType.StoredProcedure);
            return p.Get<Int32>("returnValue");

        }

        public bool ExistsMenuCode(string menuCode)
        {
            StringBuilder strsql = new StringBuilder();
            strsql.Append("select count(1)  From  sys_menu where Isdel=0 and menucode='" + menuCode+"'");
           
            return SqlHelper.ExecuteScalar<int>(strsql.ToString()) > 0 ? true : false;
        }

    }
}
